package timing.ukulele.portal.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import timing.ukulele.persistence.mapper.BaseMapper;
import timing.ukulele.portal.persistent.SysResource;
import timing.ukulele.portal.persistent.SysRole;
import timing.ukulele.portal.persistent.SysRoleResource;
import timing.ukulele.portal.persistent.SysUserRole;

import java.util.List;
import java.util.Set;

@Mapper
@Repository
public interface SysRoleMapper extends BaseMapper<SysRole> {

    @Select("select role_id as roleId from sys_user_role where user_id=#{userId} and tenant_id=#{tenantId}")
    List<SysUserRole> getUserRole(@Param("userId") Long userId, @Param("tenantId") Long tenantId);

    @Select("select role_id as roleId,user_id as userId, tenant_id as tenantId, username_ as username from sys_user_role where role_id=#{roleId}")
    IPage<SysUserRole> getRoleUserPage(IPage<SysUserRole> page, @Param("roleId") Long roleId);

    @Select("select role_id as roleId,user_id as userId, tenant_id as tenantId, username_ as username from sys_user_role where role_id=#{roleId} and username like concat('%',username,'%')")
    IPage<SysUserRole> getRoleUserPageWithUsername(IPage<SysUserRole> page, @Param("roleId") Long roleId, @Param("username") String username);


    @Select("select role_id as roleId from sys_user_role where username_=#{username} and tenant_id=#{tenantId}")
    List<Long> getUserRoleByUsername(@Param("username") String username, @Param("tenantId") Long tenantId);

    @Select("select count(role_id) from sys_user_role where user_id=#{userId} and role_id=#{roleId} and tenant_id=#{tenantId}")
    int existUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId, @Param("tenantId") Long tenantId);

    @Delete("delete from sys_user_role where user_id=#{userId} and role_id=#{roleId} and tenant_id=#{tenantId}")
    int deleteUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId, @Param("tenantId") Long tenantId);

    @Delete("delete from sys_user_role where user_id=#{userId} and tenant_id=#{tenantId}")
    int deleteUserTenantRole(@Param("userId") Long userId, @Param("tenantId") Long tenantId);

    @Insert("insert sys_user_role(user_id,role_id,tenant_id,username_) value(#{userId},#{roleId},#{tenantId},#{username})")
    int addUserRole(@Param("userId") Long userId, @Param("username") String username, @Param("roleId") Long roleId, @Param("tenantId") Long tenantId);

    @Insert("<script>"
            + "insert sys_user_role(user_id,username_, role_id, tenant_id) values"
            + "<foreach collection ='roleList' item='role' separator =','>"
            + "(#{userId},#{username},#{role},#{tenantId})"
            + "</foreach >"
            + "</script>")
    int batchAddUserTenantRole(@Param("userId") Long userId, @Param("username") String username, @Param("roleList") Set<Long> roleList, @Param("tenantId") Long tenantId);


    //###========角色菜单资源===========###//

    @Select("<script>" +
            "select resource_id as resourceId from sys_role_resource where role_id = #{roleId} and resource_id in (<foreach collection='resourceIds' item='item' index='index' separator=','> #{item}</foreach>)"
            + "</script>")
    List<Long> getRoleResource(@Param("roleId") Long roleId, @Param("resourceIds") List<Long> resourceIds);

    @Select("select r.id_ as id, r.group_id as groupId,r.resource_ as resource,r.remark_ as remark, r.method_ as method,r.deleted_ as deleted " +
            "from sys_resource r, sys_role_resource rr where r.id_=rr.resource_id and rr.role_id = #{roleId}")
    List<SysResource> getRoleResourceList(@Param("roleId") Long roleId);

    @Select("<script>" +
            "select r.id_ as id, r.group_id as groupId,r.resource_ as resource,r.remark_ as remark, r.method_ as method,r.deleted_ as deleted " +
            "from sys_resource r, sys_role_resource rr where r.id_=rr.resource_id and rr.role_id in (<foreach collection='roleIds' item='item' index='index' separator=','> #{item}</foreach>)"
            + "</script>")
    List<SysResource> getRoleResourceListByIds(@Param("roleIds") List<Long> roleIds);

    @Select("select role_id, resource_id, tenant_id from sys_role_resource where role_id = #{roleId}")
    List<SysRoleResource> getRoleResourceByRoleId(@Param("roleId") Long roleId);

    @Insert("<script>" + "insert into sys_role_resource (role_id,resource_id,tenant_id) values <foreach collection='list' item='item' index='index' separator=','>(#{item.roleId},#{item.resourceId},#{item.tenantId})</foreach>" + "</script>")
    void batchAddRoleResource(List<SysRoleResource> list);

    @Delete("delete from sys_role_resource where role_id=#{roleId}")
    void deleteOneRoleResource(@Param("roleId") Long roleId, @Param("resourceId") Long resourceId);
}
